package com.box.app.arithmetic

import kotlin.test.Test
import kotlin.test.assertEquals

private const val EXPRESSION = "(1 + (2 + 3) * -~4 + pow(x, 2) - sum(2, 6 ** 2, sum(0) ** 2) + 10) ** 2"
private val SUM = object : Callable {
    override val name: String = "sum"
    override fun call(vararg values: Number): Number = values.fold(0 as Number) { t, u -> t + u }
}

internal expect fun Number.format(digit: Int = 4): Any

class ParserTest {
    @Test
    fun testCall() {
        val parser = Parser.parse(EXPRESSION, SUM)
        assertEquals(parser.toDescribe(), EXPRESSION)
        val expected = parser.call(mapOf("x" to 10))
        val actual = 9604.0
        assertEquals(expected.format(), actual.format())
    }
}